package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 加载首页-加载更多-加载更新 三位一体
     *
     * @param dto  type=1 认为是加载更多，type=2表示加载更新
     * @param type
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //TODO
        List<ApArticle> apArticleList = this.getBaseMapper().loadArticleList(dto, type);

        return ResponseResult.okResult(apArticleList);
    }


    /**
     * 保存三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        //TODO
        //生成rabbitmq消息唯一码
        //CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        //将ArticleDto 拷贝到 Article
        ApArticle apArticle = BeanUtil.toBean(dto, ApArticle.class);

        //保存数据到文章信息表
        this.save(apArticle);

        //获取文章id
        Long apArticleId = apArticle.getId();

        //封装文章配置表
        ApArticleConfig apArticleConfig = new ApArticleConfig(apArticleId);
        //保存数据到文章配置表
        articleConfigMapper.insert(apArticleConfig);

        //封装文章内容表数据
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(apArticleId);
        apArticleContent.setContent(dto.getContent());
        articleContentMapper.insert(apArticleContent);

        //生产消息用于更新ES索引库中的数据
        apArticle.setContent(dto.getContent());

        //生成rabbitmq消息唯一码
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        rabbitTemplate.convertAndSend(ArticleConstants.ARTICLE_ES_SYNC_TOPIC,ArticleConstants.ARTICLE_ES_SYNC_ROUTINGKEY,apArticle.getId(),correlationData);

        //异步调用 生成静态文件上传到minio中
        articleFreemarkerService.buildArticleToMinIO(apArticle, dto.getContent());

        //3.返回数据,要不要返回文章id ，因为wmnews表中需要
        return ResponseResult.okResult(apArticle);

    }

    //根据文章id获取文章所有信息
    @Override
    public ResponseResult getArticleMsg(String articleId) {
        ApArticle apArticle = articleMapper.getArticleMsg(Long.valueOf(articleId));

        String jsonString = JSON.toJSONString(apArticle);

        return ResponseResult.okResult(jsonString);
    }

}
